package quicksync

import (
	"fmt"
	"time"
)

type SyncBase struct {
	name, nameZh, subType string
	queue                 *SyncQueue
	step                  SyncStepType
	entity                interface{}
	wantRun, wantSql      bool
	sqlVerifyFunc         func(string, interface{}) error
}

func NewSyncBase(name, nameZh, subType string, homedQueue *SyncQueue, step SyncStepType, wantRun, wantSql bool, verifyEntity interface{}, sqlVerifyFunc func(string, interface{}) error) *SyncBase {
	return &SyncBase{name, nameZh, subType, homedQueue, step, verifyEntity, wantRun, wantSql, sqlVerifyFunc}
}
func (s *SyncBase) GetName() string {
	return s.name
}
func (s *SyncBase) GetNameZh() string {
	return s.nameZh
}
func (s *SyncBase) GetSubType() string {
	return s.subType
}
func (s *SyncBase) GetQueue() *SyncQueue {
	return s.queue
}
func (s *SyncBase) GetStep() SyncStepType {
	return s.step
}
func (s *SyncBase) GetEntity() interface{} {
	return s.entity
}
func (s *SyncBase) ValidateSql(sql string) error {
	if s.entity == nil {
		return fmt.Errorf("未配置实体类类型 : %s", s.nameZh)
	}
	if s.sqlVerifyFunc == nil {
		return fmt.Errorf("未配置SQL校验方法 : %s", s.nameZh)
	}
	return s.sqlVerifyFunc(sql, s.entity)
}
func (s *SyncBase) WantRun() bool {
	return s.wantRun
}
func (s *SyncBase) WantSql() bool {
	return s.wantSql
}

type SyncQueue struct {
	name       string
	interval   time.Duration
	finishHook func() error
}

var syncQueueMap map[string]*SyncQueue

func GenSyncQueue(name string, interval time.Duration, finishHook func() error) *SyncQueue {
	if syncQueueMap == nil {
		syncQueueMap = make(map[string]*SyncQueue)
	}
	queue := &SyncQueue{name, interval, finishHook}
	syncQueueMap[name] = queue
	return queue
}
func (s *SyncQueue) GetName() string {
	return s.name
}
func (s *SyncQueue) GetInterval() time.Duration {
	return s.interval
}
